<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible"
  >
    <el-form
      :model="dataForm"
      :rules="dataRule"
      ref="dataForm"
      @keyup.enter.native="dataFormSubmit()"
      label-width="150px"
    >
      <el-form-item label="标题" prop="title">
        <el-input
          v-model="dataForm.title"
          placeholder="请输入标题"
        ></el-input>
      </el-form-item>
      <el-form-item label="理财产品名称" prop="productName">
        <el-input
          v-model="dataForm.productName"
          placeholder="请输入理财产品名称"
        ></el-input>
      </el-form-item>
      <el-form-item
        label="年化"
        prop="interestRate"
      >
        <el-input
          v-model="dataForm.interestRate"
          placeholder="请输入理财产品年化"
        >
          <template slot="append">
            <div>%</div>
          </template>
        </el-input>
      </el-form-item>

      <el-form-item
        label="存款时间"
        prop="depositTime"
      >
        <el-input
          v-model="dataForm.depositTime"
          placeholder="请输入存款时间"
        >
            <el-radio-group v-model="dataForm.depositType" slot="append">
              <el-radio :label="1">月</el-radio>
              <el-radio :label="2">年</el-radio>
            </el-radio-group>
        </el-input>
        
      </el-form-item>
      <el-form-item
        label="是否可以提前提取"
        prop="cashAdvance"
      >
      <el-radio-group v-model="dataForm.cashAdvance">
          <el-radio :label="0">否</el-radio>
          <el-radio :label="1">是</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item
        label="产品风险等级"
        prop="riskLevel"
      >
        <el-radio-group v-model="dataForm.riskLevel">
          <el-radio :label="1">低风险</el-radio>
          <el-radio :label="2">中风险</el-radio>
          <el-radio :label="3">高风险</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item
        label="是否自动赎回"
        prop="autoRedemption"
      >
        <el-radio-group v-model="dataForm.autoRedemption">
          <el-radio :label="0">否</el-radio>
          <el-radio :label="1">是</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="产品描述" prop="description">
        <el-input
          type="textarea"
          :autosize="{ minRows: 2, maxRows: 4}"
          placeholder="请输入内容"
          v-model="dataForm.description">
        </el-input>
      </el-form-item>
      <!-- <el-form-item label="状态" size="mini" prop="status">
        <el-radio-group v-model="dataForm.status">
          <el-radio :label="0">禁用</el-radio>
          <el-radio :label="1">正常</el-radio>
        </el-radio-group>
      </el-form-item>-->
    </el-form> 
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
export default {
  data () {
    // var validatePassword = (rule, value, callback) => {
    //   if (!this.dataForm.id && !/\S/.test(value)) {
    //     callback(new Error('密码不能为空'))
    //   } else {
    //     callback()
    //   }
    // }
    // var validateComfirmPassword = (rule, value, callback) => {
    //   if (!this.dataForm.id && !/\S/.test(value)) {
    //     callback(new Error('确认密码不能为空'))
    //   } else if (this.dataForm.password !== value) {
    //     callback(new Error('确认密码与密码输入不一致'))
    //   } else {
    //     callback()
    //   }
    // }
    // var validateEmail = (rule, value, callback) => {
    //   if (!isEmail(value)) {
    //     callback(new Error('邮箱格式错误'))
    //   } else {
    //     callback()
    //   }
    // }
    // var validateMobile = (rule, value, callback) => {
    //   if (!isMobile(value)) {
    //     callback(new Error('手机号格式错误'))
    //   } else {
    //     callback()
    //   }
    // }
    // var validateEndTime = (rule, value, callback) => {
    //   console.log(value, this.dataForm.startTime)
    //   // if (!isMobile(value)) {
    //   //   callback(new Error('手机号格式错误'))
    //   // } else {
    //   //   callback()
    //   // }
    // }
    return {
      visible: false,
      roleList: [],
      optObj: [{
        label: 'T+1工作日',
        value: 1
      }],
      dataForm: {
        id: 0,
        // sessionName: '',
        // startTime: '',
        // endTime: '',
        // status: 1,
        title: '',
        productId: '',
        productName: '',
        description: '',
        interestRate: '',
        depositTime: '',
        riskLevel: 1,
        cashAdvance: 0,
        autoRedemption: 0,
        depositType: 1
      },
      dataRule: {
        productName: [
          { required: true, message: '请输入产品名称', trigger: 'blur' }
        ],
        description: [
          { required: true, message: '请输入产品描述', trigger: 'blur' }
        ],
        title: [
          { required: true, message: '请输入标题', trigger: 'blur' }
        ],
        interestRate: [
          { required: true, message: '请输入年化', trigger: 'blur' }
        ],
        depositTime: [
          { required: true, message: '请输入存款时间', trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    init (id) {
      this.dataForm.id = id || 0
      this.$nextTick(() => {
        this.$refs['dataForm'].resetFields()
      })
      if (this.dataForm.id) {
        this.$http({
          url: this.$http.adornUrl(
            `/coupon/coupon/product/info/${this.dataForm.id}`
          ),
          method: 'get',
          params: this.$http.adornParams()
        }).then(({data}) => {
          if (data && data.code === 0) {
            this.dataForm.productId = data.product.productId
            this.dataForm.productName = data.product.productName
            this.dataForm.title = data.product.title
            this.dataForm.interestRate = data.product.interestRate
            this.dataForm.riskLevel = data.product.riskLevel
            this.dataForm.cashAdvance = data.product.cashAdvance ? 1 : 0
            let arr = data.product.depositTime.split('-')
            this.dataForm.depositType = arr[0] > 0 ? 2 : 1
            this.dataForm.depositTime = arr[0] > 0 ? arr[0] : arr[1]
            this.dataForm.description = data.product.description
            this.dataForm.autoRedemption = data.product.autoRedemption ? 1 : 0
          }
        })
      }
      this.visible = true
    },
    // 表单提交
    dataFormSubmit () {
      this.$refs['dataForm'].validate((valid) => {
        if (valid) {
          let depositTime
          if (this.dataForm.depositType === 2) {
            depositTime = `${this.dataForm.depositTime}-0`
          } else {
            depositTime = `0-${this.dataForm.depositTime}`
          }
          this.$http({
            url: this.$http.adornUrl(
              `/coupon/coupon/product/${!this.dataForm.id ? 'save' : 'update'}`
            ),
            method: 'post',
            openDefultdata: false,
            data: this.$http.adornData(!this.dataForm.id ? {
              productId: this.dataForm.id || undefined,
              productName: this.dataForm.productName,
              title: this.dataForm.title,
              interestRate: this.dataForm.interestRate,
              riskLevel: this.dataForm.riskLevel,
              cashAdvance: this.dataForm.cashAdvance,
              description: this.dataForm.description,
              autoRedemption: this.dataForm.autoRedemption,
              depositTime: depositTime
              // status: this.dataForm.id ? this.dataForm.status : undefined
            } : [{
              productId: this.dataForm.id || undefined,
              productName: this.dataForm.productName,
              title: this.dataForm.title,
              interestRate: this.dataForm.interestRate,
              riskLevel: this.dataForm.riskLevel,
              cashAdvance: this.dataForm.cashAdvance,
              description: this.dataForm.description,
              autoRedemption: this.dataForm.autoRedemption,
              depositTime: depositTime
              // status: this.dataForm.id ? this.dataForm.status : undefined
            }], false)
          }).then(({ data }) => {
            if (data && data.code === 0) {
              this.$message({
                message: '操作成功',
                type: 'success',
                duration: 1500,
                onClose: () => {
                  this.visible = false
                  this.$emit('refreshDataList')
                }
              })
            } else {
              this.$message.error(data.msg)
            }
          })
        }
      })
    }
  }
}
</script>
<style>
  /* .el-input {
    width: 30px;
  } */
  .el-input-group .el-input-group__append {
    background-color: #fff;
  }
</style>
